/*
 * invulgotracker
 * Copyright (C) Thomas Andersen 2011 <thomas.1.andersen@gmail.com>
 *
 * invulgotracker is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * invulgotracker is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef TIMEUSEDTHREAD_H
#define TIMEUSEDTHREAD_H

#define _MULTI_THREADED
#include <pthread.h>

#include "task-time.h"
#include "time-view.h"
#include "task-view.h"
#include "taskid.h"
#include "day.h"
#include <list>

using std::list;

/**
  * Calculate total time used for tasks in view.
  * This is done in a background thread.
  */
class TimeUsedThread : IDayLoad
{
public:
    TimeUsedThread();
    virtual ~TimeUsedThread();

    int Start();
    void Data(gchar *name, gchar *timeStr, TaskId id);
    void UpdateView(TimeView &timeView, TaskView &taskView);
    void UpdateParents(TaskView &taskView);
    bool IsDone();
    void Calc();
    bool Begin();
    bool Next();
    const TaskId &GetTaskId();
    const TaskTime &GetTaskTime();
private:
    void AddToParent(TaskView &taskView, const TaskId &id, const TaskTime &diff);
    static void *Run(void *t);
    struct task {
        TaskId id;
        TaskTime usedTime;
    };

    pthread_mutex_t statusMutex;
    bool done;
    bool killed;
    bool isThread;
    pthread_t thread;
    TaskId prevId;
    TaskTime *prevTime;
    list<task> taskList;
    list<task> taskListTmp;
    list <task>::const_iterator iter;
};

#endif // TIMEUSEDTHREAD_H
